package com.xgf.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * Servlet implementation class testSmsAction
 */
@WebServlet("/testSmsAction")
public class testSmsAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public testSmsAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println(request.getParameter("phone")+"电话");
		String phone = request.getParameter("phone");
		String name = "徐先生";
		String url="http://gw.api.taobao.com/router/rest";
		String appkey="23564495";
		String secret="1be29274b54185fa125055c6e9207424";
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("123456");//回调函数可以发送完了以后返回值
		req.setSmsType("normal");
		req.setSmsFreeSignName("智慧社区幸福蔡关");//短信签名
		req.setSmsParamString("{\"name\":\""+name+"\"}");
		String dispNum = phone;
		phone+=",15213438627";
		req.setRecNum(phone.toString());
		req.setSmsTemplateCode("SMS_33840229");
		try {
			System.out.println("测试信息");
//			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
//			System.out.println(rsp.getBody());
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
	    out.println("此次发送的号码为："+dispNum+",发送成功请注意查收......");
	    response.setHeader("refresh", "3;url=detail.jsp");
	}

}
